import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SocketServerMain {
    static List<SocketServer> list = new ArrayList();
    public static void main(String[] args) throws IOException {
        ServerSocket server= new ServerSocket(9999);
        server.setSoTimeout(15000);
        Scanner sc = new Scanner(System.in);
        while (true){
            if(list.size()==0){
                System.out.println("没有人聊天，输入0关闭服务器，或输入其他启动服务器:");
                if(sc.next().equals("0")){
                    break;
                }
            }
            Socket socket;
            try{
                socket = server.accept();
            }catch (IOException e){
                continue;
            }
            list.add(new SocketServer(socket));
            System.out.println(socket.getRemoteSocketAddress()+"加入服务器");
        }
    }
}
